//
//  visitView.swift
//  weibo1
//
//  Created by 李进 on 15/10/15.
//  Copyright © 2015年 李进. All rights reserved.
//

import UIKit

class visitView: UIView {
    
    //MARK:设置view的空间的信息
    func setupInfo(messageInfo: String , homeImageName: String?){
        
        messageLabel.text = messageInfo
        
        guard let homeImageName = homeImageName else{
            animationStart()
            return
        }
        
        HomeView.image = UIImage(named: homeImageName)
        
        iconView.hidden = true
        
        sendSubviewToBack(maskIconView)
        
    }
    
    // MARK:构造函数
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        self.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        //记载视图
        setViewBord()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
        setViewBord()
    }


    // MARK:懒加载view中的控件
    /// 最底层图片视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    /// 房子的图片视图
    private lazy var HomeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    private lazy var messageLabel: UILabel = UILabel(titlt: "关注一些人，回这里看看有什么惊喜", fontsize: 14, fontcolor: UIColor.grayColor())
    
    //创建登录按钮和注册按钮的懒加载
    /// 注册按钮
     lazy var reginBut: UIButton = UIButton(tittle: "注册", backImageName: "common_button_white_disable", fontsize: 18, fontcolcr: UIColor.orangeColor())
    /// 登录按钮
     lazy var loginBut: UIButton = UIButton(tittle: "登录", backImageName: "common_button_white_disable", fontsize: 18, fontcolcr: UIColor.grayColor())
    
    //设置动画
    func animationStart() {
        
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        
        //anima.byValue = 0
        
        anima.fromValue = 0
        
        anima.toValue = 2 * M_PI
        
        anima.duration = 20
        
        anima.repeatCount = MAXFLOAT
        
        //在这里必须实现不能移除动画，不然不会一直有动画
        anima.removedOnCompletion = false
        
        //添加动画.是添加在layer的上面
        iconView.layer.addAnimation(anima, forKey: nil)
        
        
    }
    
    
}

    //MARK:设置界面
extension visitView{
    /// 添加控件
    func setViewBord (){
        
        //添加图层
        addSubview(iconView)
        
        addSubview(maskIconView)
        
        addSubview(HomeView)
        
        addSubview(messageLabel)
        
        addSubview(reginBut)
        
        addSubview(loginBut)
        
        
        
        //手写自动布局，必须取消系统的自动布局，这样手写的自动布局才会生效
        for vc in subviews{
            vc.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //1.>图标的自动布局
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1.0, constant: -50))
        
        //2.>房子的自动布局
        addConstraint(NSLayoutConstraint(item: HomeView, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: HomeView, attribute: .CenterY, relatedBy: .Equal, toItem: iconView, attribute: .CenterY, multiplier: 1.0, constant: 0))
        
        //3.>遮罩的自动布局
        let dicview = ["maskIconView":maskIconView, "visit":self]
        let IND = ["visith": 250]
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[maskIconView]-0-|", options: [], metrics: nil, views: dicview))
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[maskIconView]-(visith)-|", options: [], metrics: IND, views: dicview))
        
        //4.>信息
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .CenterX, relatedBy: .Equal, toItem: iconView, attribute: .CenterX, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 224))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Top, relatedBy: .Equal, toItem: iconView, attribute: .Bottom, multiplier: 1.0, constant: 16))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        
        
        //5.>注册按钮
        addConstraint(NSLayoutConstraint(item: reginBut, attribute: .Left, relatedBy: .Equal, toItem: messageLabel, attribute: .Left, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: reginBut, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 5))
        addConstraint(NSLayoutConstraint(item: reginBut, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: reginBut, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
        //6.>登录按钮
        addConstraint(NSLayoutConstraint(item: loginBut, attribute: .Right, relatedBy: .Equal, toItem: messageLabel, attribute: .Right, multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBut, attribute: .Top, relatedBy: .Equal, toItem: messageLabel, attribute: .Bottom, multiplier: 1.0, constant: 5))
        addConstraint(NSLayoutConstraint(item: loginBut, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBut, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 35))
        
    }
    
    
    
}